package com.example.hanfu_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hanfu_backend.entity.Hanfu;
import com.example.hanfu_backend.entity.Goods;
import com.example.hanfu_backend.entity.HanfuShop;
import com.example.hanfu_backend.entity.HanfuType;
import com.example.hanfu_backend.mapper.HanfuMapper;
import com.example.hanfu_backend.mapper.GoodsMapper;
import com.example.hanfu_backend.mapper.HanfuShopMapper;
import com.example.hanfu_backend.mapper.HanfuTypeMapper;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/hanfu")
public class HanfuController {
    @Autowired
    HanfuMapper hanfuMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    HanfuShopMapper hanfuShopMapper;
    @Autowired
    HanfuTypeMapper hanfuTypeMapper;

    @GetMapping("/getAllHanfus")
    @Operation(summary = "不分页查询所有汉服")
    public List<Hanfu> getAllHanfus() {
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfu_statu",1);
        return hanfuMapper.selectList(queryWrapper);
    }

    @GetMapping("/getAllHanfusByName")
    @Operation(summary = "通过汉服名称不分页模糊查询所有汉服")
    public List<Hanfu> getAllHanfusByName(@RequestParam("hanfuName")String hanfuName) {
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("hanfu_name",hanfuName);
        return hanfuMapper.selectList(queryWrapper);
    }

    @GetMapping("/getAllHanfusByShopName")
    @Operation(summary = "通过店铺名称不分页模糊查询所有汉服")
    public List<Hanfu> getAllHanfusByShopName(@RequestParam("hanfuShopName")String hanfuShopName) {
        System.out.println(hanfuShopName);
        QueryWrapper<HanfuShop> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like("hanfushop_name",hanfuShopName);
        List<HanfuShop> result = hanfuShopMapper.selectList(queryWrapper1);
        System.out.println(result);
        List<Integer> hanfuShopIds = result.stream().map(HanfuShop::getHanfuShopId).collect(Collectors.toList());
        System.out.println(hanfuShopIds.toArray());
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("hanfushop_id",hanfuShopIds);
        return hanfuMapper.selectList(queryWrapper);
    }

    @GetMapping("/getAllHanfusByTypeName")
    @Operation(summary = "通过种类名称不分页模糊查询所有汉服")
    public List<Hanfu> getAllHanfusByTypeName(@RequestParam("hanfuTypeName")String hanfuTypeName) {
        QueryWrapper<HanfuType> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like("hanfutype_name",hanfuTypeName);
        List<HanfuType> result = hanfuTypeMapper.selectList(queryWrapper1);
        List<Integer> hanfuTypeIds = result.stream().map(HanfuType::getHanfuTypeId).collect(Collectors.toList());
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("hanfutype_id",hanfuTypeIds);
        return hanfuMapper.selectList(queryWrapper);
    }

    @GetMapping("/getHanfu")
    @Operation(summary = "分页查询所有汉服")
    public Map<String,Object> getHanfus(@RequestParam("pageNum")Integer pageNum, @RequestParam("pageSize")Integer pageSize) {
        Page<Hanfu> page = new Page<>(pageNum,pageSize); // 创建分页对象
        // 设置分页查询条件
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("hanfu_id");
        IPage<Hanfu> iPage = hanfuMapper.selectPage(page,queryWrapper); // 执行分页查询操作
        // 封装分页查询结果
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getNewHanfu")
    @Operation(summary = "得到上新汉服")
    public Map<String,Object> getNewHanfu(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
        Page<Hanfu> page = new Page<>(pageNum,pageSize); // 创建分页对象
        // 设置分页查询条件
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfu_statu",1).orderByDesc("hanfu_id"); // 已上架的且倒序
        IPage<Hanfu> iPage = hanfuMapper.selectPage(page,queryWrapper); // 执行分页查询操作
        // 封装分页查询结果
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getHotHanfu")
    @Operation(summary = "得到热门汉服")
    public Map<String,Object> getHotHanfu(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
        // 先查找热门的四件汉服编号
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("hanfu_id","SUM(order_sum) as count").groupBy("hanfu_id").orderByDesc("count").last("LIMIT 4");
        List<Map<String, Object>> result = goodsMapper.selectMaps(queryWrapper);
        // 获取汉服ID列表
        List<Integer> hanfuIds = result.stream().map(item -> (Integer)item.get("hanfu_id")).collect(Collectors.toList());
        Page<Hanfu> page = new Page<>(pageNum,pageSize); // 创建分页对象
        // 设置分页查询条件
        QueryWrapper<Hanfu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("hanfu_id",hanfuIds);
        IPage<Hanfu> iPage = hanfuMapper.selectPage(page,queryWrapper1); // 执行分页查询操作
        // 封装分页查询结果
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getHanfuById")
    @Operation(summary = "通过汉服ID分页查询汉服")
    public Map<String,Object> queryHanfuById(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("hanfuId")Integer hanfuId) {
        Page<Hanfu> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfu_id",hanfuId);
        IPage<Hanfu> iPage = hanfuMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getHanfuByStatu")
    @Operation(summary = "通过商品状态分页查询汉服")
    public Map<String,Object> queryHanfuByStatu(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("hanfuStatu")Integer hanfuStatu) {
        Page<Hanfu> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfu_statu",hanfuStatu);
        IPage<Hanfu> iPage = hanfuMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getHanfuByName")
    @Operation(summary = "通过汉服名称分页模糊查询汉服")
    public Map<String,Object> queryHanfuByName(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("hanfuName")String hanfuName) {
        Page<Hanfu> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Hanfu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("hanfu_name",hanfuName);
        IPage<Hanfu> iPage = hanfuMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @PostMapping("/insertHanfu")
    @Operation(summary = "添加汉服")
    public int insertShop(@RequestBody Hanfu hanfu) {
        return hanfuMapper.insert(hanfu);
    }

    @DeleteMapping("/deleteHanfu")
    @Operation(summary = "根据ID删除某一汉服")
    public int deleteShop(@RequestParam("hanfuId")Integer hanfuId) {
        return hanfuMapper.deleteById(hanfuId);
    }

    @PutMapping("/updateHanfu")
    @Operation(summary = "根据Id修改某一汉服")
    public int updateShop(@RequestBody Hanfu hanfu) {
        return hanfuMapper.updateById(hanfu);
    }
}
